<?php

class ArticleSidebarPanel extends DefaultSidebarPanel
{    
    public function GetGeneratedContent( $panelInfo, TemplateEngine $tmpl, $pageId )
    {
        $articles = Core::ResourceManager()->GetArticles();
        $articlesCount = count($articles);
        $articleIndex = $articlesCount == 0 ? 0 : $this->seed % $articlesCount;
        $tmplVar = array("ARTICLE_DATA" => self::GenerateArticle($articleIndex));
        $cacheId = "html/_sidebar_panels/". $panelInfo['id'] . "_$articleIndex";
        
        return $this->GenerateContent($cacheId, $panelInfo, $tmpl, $tmplVar);
    }
    
    private static function GenerateArticle( $articleIndex )
    {
        $content = '';
        $articles = ArticleTable::GetItemsByType( ArticleType::ARTICLE, Core::GetLanguage(), true );

        if ( isset($articles[$articleIndex]) )
        {
            $article = $articles[$articleIndex];

            $tmpl = Core::CreateTemplateEngine();
            $tmpl->define("article", "include/articles/article.tpl");
            $tmpl->set('TITLE',         $article->GetTitle());
            $tmpl->set('DESCRIPTION',   $article->GetExcerpt());
            $tmpl->set('LINK',          $article->GetUrl());
            $tmpl->parse("article", "article");

            $content = $tmpl->getGenerated();
        }

        return $content;
    } 
}

?>
